﻿using System;
using System.Collections.Generic;
using System.Linq;
using SdmxMl.Common;
using System.Text;

namespace ChopPlugin
{
    public class ChopRefParser
    {

        const char RANGE_SEPARATOR = '-';
        const char GROUP_SEPARATOR = ',';
        const char END_MARKER = ')';
        const char START_MARKER = '(';

        public static List<string> GetListOfReferedCode(string strReference)
        {
            List<string> lislRefCodes = new List<string>();

            if (string.IsNullOrEmpty(strReference) == false &&
                strReference[strReference.Length - 1] == END_MARKER)
            {
                int ndx = strReference.LastIndexOf(START_MARKER);
                if (ndx >= 0)
                {
                    string target = strReference.Substring(ndx + 1);
                    target = target.Replace(")", "");
                    if (target.Contains('.'))
                    {
                        if (LangSupport.CurLang == "de")
                        {
                            target = target.Replace("u ff.", "");
                            target = target.Replace("ff.", "");
                            target = target.Replace(" ff", "");
                        }
                        else if (LangSupport.CurLang == "fr")
                        {
                            target = target.Replace(".-", "");
                            target = target.Replace("ss", "");
                        }
                        else if (LangSupport.CurLang == "it")
                        {
                            target = target.Replace(".-", "");
                            target = target.Replace("segg.", "");
                        }
                        string[] groups = target.Split(GROUP_SEPARATOR);
                        foreach (string grp in groups)
                        {
                            if (grp.Trim().Length > 0)
                            {
                                string[] range = grp.Trim().Split(RANGE_SEPARATOR);
                                if (range.Length > 0)
                                {
                                    if (range.Length == 2)
                                    {
                                        string start = range[0].Trim().Replace(".", "").TrimStart('Z');
                                        string stop = range[1].Trim().Replace(".", "").TrimStart('Z'); ;
                                        if (start.Length > 0)
                                            lislRefCodes.Add(start);
                                        if (stop.Length > 0)
                                            lislRefCodes.Add(stop);

                                        /*
                                        int codeStart, codeEnd;
                                        if (int.TryParse(start, out codeStart) && 
                                            int.TryParse(stop, out codeEnd))
                                        {
                                            for (int c = codeStart; c <= codeEnd; c++)
                                            {
                                                string co = c.ToString().PadLeft(start.Length, '0');
                                                lislRefCodes.Add(co);
                                            }
                                        }
                                        else
                                            throw new ApplicationException("unable to parse range for: " + start + " " + stop);
                                         * */
                                    }
                                    else
                                    {
                                        string cd = range[0].Trim().Replace(".", "").TrimStart('Z');
                                        if (cd.Length > 0)
                                            lislRefCodes.Add(cd);
                                    }
                                }
                            }

                        }
                    }
                }
            }

            return lislRefCodes;
        }
    }

    public class ReferenceDiagnostic
    {
        public bool Found { get; set; }
        public string Source { get; set; }
        public string Reference { get; set; }
        public string Type { get; set; }
        public string AnnotText { get; set; }

        public ReferenceDiagnostic(bool f, string src, string codeRef, SdmxMl.Common.Annotation a)
        {
            Found = f;
            Source = PdfChop.BeautifyCodeId(src);
            Reference = PdfChop.BeautifyCodeId(codeRef);
            Type = ExportGridCreator.GetExportTypeOfAnnotation(a);
            AnnotText = a.Text.ToString();
        }
    }
}
